package com.xteam.xnetworkdisk.utils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class Tools {

    public static String INQUIRETYPE_PROPERTY="property";
    public static String INQUIRETYPE_KEYWORD="keyword";
    private static final Pattern ILLEGAL_CHAR_PATTERN = Pattern.compile("[<>:\"/\\|?*]");


    public static Boolean isQQEmail(String email){
        final String QQ_EMAIL_REGEX = "[0-9]{5,10}@qq.com";
        Pattern pattern = Pattern.compile(QQ_EMAIL_REGEX);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
    public static Boolean isNeteaseEmail(String email) {
        final String NETEASE_EMAIL_REGEX = "[a-zA-Z0-9._%+-]+@(163|126)\\.com";
        Pattern pattern = Pattern.compile(NETEASE_EMAIL_REGEX);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    public static String getSuffix(String fileName){
        String fileExtension = "";
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex != -1) {
            fileExtension = fileName.substring(dotIndex);
        }
        return fileExtension;
    }
    public static String sanitizeFileName(String fileName) {
        return ILLEGAL_CHAR_PATTERN.matcher(fileName).replaceAll("_");
    }


    public static String[] extractFilename(String url) {
        // 解析 URL
        URI uri = null;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        // 提取路径部分
        String path = uri.getPath();
        // 获取文件名部分（包含扩展名）
        String filenameWithExtension = path.substring(path.lastIndexOf('/') + 1);
        // 分离文件名和扩展名
        String[] parts = filenameWithExtension.split("\\.(?=[^\\.]+$)");
        String filename = parts[0];
        String extension = parts.length > 1 ? "." + parts[1] : "";
        return new String[]{filename, extension};
    }

    public static String[] splitFileName(String fileName) {
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1 || dotIndex == 0 || dotIndex == fileName.length() - 1) {
            // 如果没有找到点，或者点在字符串的开头或结尾
            return new String[]{fileName, ""};
        } else {
            String name = fileName.substring(0, dotIndex);
            String extension = fileName.substring(dotIndex + 1);
            return new String[]{name, extension};
        }
    }
}
